package com.demo.modalframe3;

import cn.hutool.core.util.StrUtil;
import com.demo.modalframe3.listener.*;
import com.demo.modalframe3.utils.AppendTextFrame;
import com.demo.modalframe3.utils.GraphicsUtil;
import com.demo.modalframe3.utils.UIUtil;
import com.esotericsoftware.tablelayout.swing.Table;

import javax.swing.*;
import javax.swing.event.DocumentListener;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.time.LocalDateTime;

public class Frame1 extends JFrame implements AppendTextFrame {

    private JTextField inputTextField;
    private JTextField filePathField;
    private JSpinner spinner;
    private JTextPane textPane;

    public Frame1() {
        initFrame();

        this.setTitle("Frame 1(子对话框关闭后递增数值)");
        this.setMinimumSize(new Dimension(GraphicsUtil.Frame_Width, GraphicsUtil.Frame_Height));
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.addWindowListener(new CloseWindowListener());
        this.addHierarchyListener(new FrameHierarchyListener());
        this.addMouseListener(new FrameMouseListener());
        this.addWindowStateListener(new FrameStateListener());
        this.addComponentListener(new FrameComponentListener());
        this.addFocusListener(new FrameFocusListener());
    }

    private void initFrame() {
        JButton button1 = new JButton("Modalless Dialog");
        button1.addActionListener(e -> {
            showModelessDialog();
        });

        JButton button2 = new JButton("Document Dialog");
        button2.addActionListener(e -> {
            showDocumentModalDialog();
        });

        JButton button3 = new JButton("Application Dialog");
        button3.addActionListener(e -> {
            showApplicationModalDialog();
        });

        inputTextField = new JTextField();
        inputTextField.setEditable(false);
        JButton inputButton = new JButton("获取输入文本");
        inputButton.addActionListener(e -> {
            getInputText();
        });

        filePathField = new JTextField();
        filePathField.setEditable(false);
        JButton chooseFileButton = new JButton("打开文件");
        chooseFileButton.addActionListener(e -> {
            getOpenFilePath();
        });

        spinner = new JSpinner();
        spinner.setValue(0);
        spinner.setEnabled(false);

        textPane = new JTextPane();
        textPane.setEditable(false);

        Table contentTable = UIUtil.defaultTable();
        contentTable.addCell("打开一个非模态对话框:").uniform();
        contentTable.addCell(button1).left().row();

        contentTable.addCell("打开一个Document模态对话框:").uniform();
        contentTable.addCell(button2).left().row();

        contentTable.addCell("打开一个Application模态对话框:").uniform();
        contentTable.addCell(button3).left().row();

        contentTable.addCell(inputTextField).uniform().fillX();
        contentTable.addCell(inputButton).left().row();

        contentTable.addCell(filePathField).uniform().fillX();
        contentTable.addCell(chooseFileButton).left().row();

        contentTable.addCell("子对话框关闭后改变的数值:").uniform();
        contentTable.addCell(spinner).left().row();

        contentTable.addCell(new JScrollPane(textPane)).colspan(2).expand().fill();

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(contentTable, BorderLayout.CENTER);
    }

    private void showModelessDialog() {
        JDialog dialog = new JDialog(this, "非模态对话框",
                Dialog.ModalityType.MODELESS);
        dialog.getContentPane().add(new JTextArea("这是一个非模态对话框"));
        dialog.setMinimumSize(new Dimension(GraphicsUtil.Dialog_Width, GraphicsUtil.Dialog_Height));
        dialog.setLocationRelativeTo(this);
        dialog.addWindowListener(new DialogCloseListener(this));
        dialog.setVisible(true);
        appendText2TextPane("非模态对话框 setVisible(true)返回");
    }

    private void showDocumentModalDialog() {
        JDialog dialog = new JDialog(this, "Document模态对话框",
                Dialog.ModalityType.DOCUMENT_MODAL);
        dialog.getContentPane().add(new JTextArea("这是一个Document模态对话框"));
        dialog.setMinimumSize(new Dimension(GraphicsUtil.Dialog_Width, GraphicsUtil.Dialog_Height));
        dialog.setLocationRelativeTo(this);
        dialog.addWindowListener(new DialogCloseListener(this));
        dialog.setVisible(true);
        increaseNumber();
        appendText2TextPane("Document模态对话框 setVisible(true)返回");
        dialog.dispose();
    }

    private void showApplicationModalDialog() {
        JDialog dialog = new JDialog(this, "Application模态对话框",
                Dialog.ModalityType.APPLICATION_MODAL);
        dialog.getContentPane().add(new JTextArea("这是一个Application模态对话框"));
        dialog.setMinimumSize(new Dimension(GraphicsUtil.Dialog_Width, GraphicsUtil.Dialog_Height));
        dialog.setLocationRelativeTo(this);
        dialog.addWindowListener(new DialogCloseListener(this));
        dialog.setVisible(true);
        increaseNumber();
        appendText2TextPane("Application模态对话框 setVisible(true)返回");
        dialog.dispose();
    }

    private void getInputText() {
        String text = JOptionPane.showInputDialog(this, "输入文本");
        if (text != null) {
            inputTextField.setText(text);
        }
    }

    private void getOpenFilePath() {
        JFileChooser chooser = new JFileChooser("选择文件");
        int value = chooser.showOpenDialog(this);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (value == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            filePathField.setText(file.getAbsolutePath());
        }
    }

    @Override
    public void appendText2TextPane(String text) {
        String insertText = LocalDateTime.now().toString() + "\t" + text + "\r\n";
        String paneText = textPane.getText();
        int count = StrUtil.count(paneText, "\r\n");

        SimpleAttributeSet attributeSet = new SimpleAttributeSet();
        StyleConstants.setForeground(attributeSet, count % 2 == 0 ? Color.red : Color.blue);
        StyledDocument document = textPane.getStyledDocument();

        try {
            document.insertString(document.getLength(), insertText, attributeSet);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void increaseNumber() {
        int value = (int)spinner.getValue();
        spinner.setValue(value + 1);
    }
}
